<template>
  <div class="app-container">
    <nav-bar />
    <div class="content-container">
      <!-- 主页路由 -->
      <template v-if="$route.path === '/'">
        <section id="home" class="section section-home">
          <div class="section-content">
            <home-view />
          </div>
          <div class="section-overlay"></div>
        </section>
        
        <section id="cases" class="section section-cases">
          <div class="section-content">
            <cases-view />
          </div>
          <div class="section-overlay"></div>
        </section>
        <section id="faq" class="section section-faq">
          <div class="section-content">
            <faq-view />
          </div>
          <div class="section-overlay"></div>
        </section>
        <section id="footer" class="section section-footer">
          <footer-component />
        </section>
      </template>
      
      <!-- 其他路由视图 -->
      <router-view v-else />
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, nextTick } from 'vue'
import NavBar from './components/NavBar.vue'
import HomeView from './views/Home.vue'
import CasesView from './views/Cases.vue'
import FaqView from './views/FAQ.vue'
import FooterComponent from './components/Footer.vue'

// 添加滚动监听以触发动画
onMounted(() => {
  // 使用requestAnimationFrame确保动画流畅
  nextTick(() => {
    const sections = document.querySelectorAll('.section')
    
    // 优化Intersection Observer配置
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          // 使用requestAnimationFrame优化动画性能
          requestAnimationFrame(() => {
            entry.target.classList.add('active')
            
            // 如果是案例部分，确保内容可见
            if (entry.target.id === 'cases') {
              const showcaseItems = document.querySelectorAll('.showcase-item')
              if (showcaseItems.length > 0) {
                // 限制同时动画的数量，减少性能压力
                const visibleItems = Array.from(showcaseItems).slice(0, 5)
                visibleItems.forEach((item, index) => {
                  setTimeout(() => {
                    requestAnimationFrame(() => {
                      item.classList.add('active')
                    })
                  }, index * 150) // 减少延迟时间
                })
              }
            }
          })
        }
      })
    }, {
      threshold: 0.1, // 降低阈值，减少计算量
      rootMargin: '0px 0px -10% 0px' // 提前触发
    })

    // 批量处理DOM操作，减少重排和重绘
    requestAnimationFrame(() => {
      sections.forEach(section => {
        observer.observe(section)
        // 默认激活当前可见的部分
        if (section.getBoundingClientRect().top < window.innerHeight * 0.5) {
          section.classList.add('active')
        }
      })
    })
  })
})
</script>

<style>
:root {
  --primary-color: #42b883;
  --text-color: #333;
  --background-color: #f0f9ff;
  --bg-color: #f5f5f5;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow-x: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--text-color);
  background-color: #000;
  will-change: scroll-position; /* 提示浏览器优化滚动 */
}

#app {
  height: 100vh;
  width: 100vw;
  overflow-x: hidden;
}

.app-container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

.content-container {
  flex: 1;
  width: 100%;
  height: calc(100vh - 64px);
  overflow-y: auto;
  overflow-x: hidden;
  scroll-snap-type: y proximity; /* 改为proximity，使滚动更自然 */
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch; /* 在iOS上提供惯性滚动 */
  overscroll-behavior: contain; /* 防止滚动传播 */
  will-change: transform; /* 提示浏览器优化变换 */
}

.section {
  width: 100vw;
  height: 100vh;
  scroll-snap-align: start;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  will-change: transform, opacity; /* 提示浏览器优化这些属性的变化 */
}

.section-cases {
  overflow-y: auto;
  height: auto;
  min-height: 100vh;
  scroll-snap-align: none; /* 移除内部滚动区域的snap对齐 */
  position: relative;
  background-color: #0a0a0a; /* 稍微变亮的黑色背景 */
  color: #fff;
}

.section-cases::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to bottom, #000, transparent);
  pointer-events: none;
  z-index: 5;
}

.section-cases::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to top, #000, transparent);
  pointer-events: none;
  z-index: 5;
}

.section-faq {
  overflow-y: auto;
  height: auto;
  min-height: 100vh;
  scroll-snap-align: none; /* 移除内部滚动区域的snap对齐 */
  position: relative;
  background-color: #0a0a0a; /* 稍微变亮的黑色背景 */
  color: #fff;
}

.section-faq::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to bottom, #000, transparent);
  pointer-events: none;
  z-index: 5;
}

.section-faq::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to bottom, transparent, #000);
  pointer-events: none;
  z-index: 10;
}

.section-footer {
  height: auto;
  min-height: 100vh;
  scroll-snap-align: start;
  overflow: hidden;
  position: relative;
}

.section-footer::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 150px;
  background: linear-gradient(to top, transparent, #000);
  pointer-events: none;
  z-index: 10;
}

.section-content {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  opacity: 0;
  transform: translateY(30px);
  transition: opacity 0.6s ease, transform 0.6s ease; /* 简化过渡效果 */
}

.section.active .section-content {
  opacity: 1;
  transform: translateY(0);
}

.section-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.4) 100%);
  z-index: 1;
  opacity: 0.5;
  transition: opacity 0.4s ease; /* 简化过渡效果 */
  will-change: opacity; /* 提示浏览器优化不透明度变化 */
}

.section-home {
  background: url(/image.png) no-repeat center center;
  background-size: cover;
  color: #fff;
  transform: translateZ(0); /* 启用GPU加速 */
}

.section-home::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: inherit;
  transform: translateZ(-1px) scale(2);
  z-index: -1;
  backface-visibility: hidden; /* 减少3D渲染工作量 */
}

@media (max-width: 768px) {
  .section-content {
    padding: 0 16px;
  }
}
</style>
